/*=============================================================
(c) all rights reserved

GraydonDataServices examples

================================================================*/
using System;
using System.Web.Services.Protocols;

/**
 * @author denisreilly
 *
 * Runnable .net 2.0 client for Graydon Company Data Webservice
 * 
 * Operation : GetDemoCompanies
 * 
 * Argument0 = Partner Userid
 * Argument1 = Partner Password
 * Argument2 = Country
 */
class DemoCompanies{   
   public static void Main(string[] args)
   {
      
       try
	   {
	      string partnerUserid         = null;
	      string partnerPassword	   = null;
	      string country		       = null;	
	      
	      // retrieve arguments
	      if (args.Length == 0)
			throw new Exception("Invalid arguments: Please specify match arguments");
			
	      // populate match parameters with arguments
	      partnerUserid      = args[0].Trim();
	      partnerPassword    = args[1].Trim();
	      country    	     = args[2].Trim();
	      
	      GraydonCompanyData                 graydonCompanyData               = new GraydonCompanyData();
	      GetDemoCompanies_ResultType        getDemoCompanies_Result          = null;
	      
	      // create the authentication parameters
	      Authentication_ParametersType authentication_Parameters                                   = new Authentication_ParametersType();
	      authentication_Parameters.PartnerUserId                                                   = partnerUserid;
	      authentication_Parameters.PartnerPassword                                                 = partnerPassword;
	      
	      // create the parameters
	      GetDemoCompanies_ParametersType getDemoCompanies_Parameters                               = new GetDemoCompanies_ParametersType();
	      getDemoCompanies_Parameters.Authentication_Parameters                                     = authentication_Parameters;
	      getDemoCompanies_Parameters.Country                                                       = country;
	      
	      // Begin the Async call to "getDemoCompanies"
	      IAsyncResult ar = graydonCompanyData.BegingetDemoCompanies(getDemoCompanies_Parameters, null, graydonCompanyData);
	      
	      // wait for response
	      ar.AsyncWaitHandle.WaitOne();
	      
	      try
          {
	         // get the results
	         getDemoCompanies_Result = graydonCompanyData.EndgetDemoCompanies(ar);
          }
          catch (SoapException soapE)
          {
            Console.WriteLine(" ");
            // SOAP 1.1 Fault info
            Console.WriteLine("SOAP 1.1 fault...");
            Console.WriteLine("Code:/t/t" + soapE.Code.ToString());
            Console.WriteLine("Actor:/t/t" + soapE.Actor);
            Console.WriteLine("Detail:/t/t" + soapE.Detail.InnerXml);
            throw new Exception("Fault occurred whilst accessing the webservice.");
          }      
          
         // get the service log details
         serviceLog(getDemoCompanies_Result.Service_Log);
         
		 // check if result has been populated
		 if (getDemoCompanies_Result.DemoCompanies != null)
		 {		
			// get the country company match methods
			CountryAndCompanyType[] countryAndCompany = getDemoCompanies_Result.DemoCompanies;
			
			for (int x = 0; x < countryAndCompany.Length; x++)
			{
				Console.WriteLine("Demo Companies:");
				Console.WriteLine("Country\t\t\t: " + countryAndCompany[x].Country);	
				
				// get the companies
				CompanyType[] company = countryAndCompany[x].Companies;
				for (int y = 0; y < company.Length; y++)
				{
					Console.WriteLine("  Company: ");
					Console.WriteLine("  Name\t\t\t: " + company[y].Name);	
					if (company[y].RegisteredAddress != null)
						Console.WriteLine("  Registered Address\t: " + company[y].RegisteredAddress.EntireAddress);
					if (company[y].TradingAddress != null)
						Console.WriteLine("  Trading Address\t: " + company[y].TradingAddress.EntireAddress);
					Console.WriteLine("  Legal Form\t\t: " + company[y].LegalForm);
					Console.WriteLine("  Email\t\t\t: " + company[y].Email);
					Console.WriteLine("  Telephone\t\t: " + company[y].Telephone);
					Console.WriteLine("  Fax\t\t\t: " + company[y].Facsimile);
					Console.WriteLine("  Company Match Id\t: " + company[y].CompanyMatchIdentifier);
					
					// get the identifiers
					if (company[y].CompanyIdentifiers != null)
					{
						CompanyIdentifierType[] companyIdentifier = company[y].CompanyIdentifiers;
						for (int z = 0; z < companyIdentifier.Length; z++)
						{
							Console.WriteLine("  Identifier: ");
							Console.WriteLine("    Type\t\t: " + companyIdentifier[z].Type);
							Console.WriteLine("    Identifier\t\t: " + companyIdentifier[z].Identifier);
						}
					}
					
					// get the Activities
					if (company[y].Activites != null)
					{
						ActivityType[] activity = company[y].Activites;
						for (int z = 0; z < activity.Length; z++)
						{
							Console.WriteLine("  Activity: ");
							Console.WriteLine("    Type\t\t: " + activity[z].Type);
							Console.WriteLine("    Code\t\t: " + activity[z].Code);
							Console.WriteLine("    Description\t\t: " + activity[z].Description);
						}
					}
				}
				Console.WriteLine(" ");
			}
		}
	  }  
      catch (Exception e)
      {
	     Console.WriteLine("Exception : " + e);
      }

   }
   
   	/**
	* Service log output
	*
	*/
	private static void serviceLog(Service_LogType service_Log)
	{
		Console.WriteLine("Service_Log :");																	
		Console.WriteLine("  PartnerUserId\t\t: " + service_Log.PartnerUserId);												    // output partner userid
		Console.WriteLine("  SessionId\t\t: " + service_Log.SessionID);														    // output sessionId
		Console.WriteLine("  Transaction Identifier: " + service_Log.TransactionIdentifier);									// output transaction identifier
		Console.WriteLine("  Request Date\t\t: " + "{0:d}", service_Log.RequestTimestamp.Date);				                    // output request date
		Console.WriteLine("  Request Time\t\t: " + "{0:t}", service_Log.RequestTimestamp.Time);							        // output request time
		Console.WriteLine("  Response Date\t\t: " + "{0:d}", service_Log.ResponseTimestamp.Date);				                // output response date
		Console.WriteLine("  Response Time\t\t: " + "{0:t}", service_Log.ResponseTimestamp.Time);								// output response time
		Console.WriteLine(" ");
	}   
   
}